enterprisePatent<template>
  <div class="enterprisePatent">
    <div class="form">
      <el-form model="form" inline>
        <el-form-item label="专利名称">
          <el-input v-model="form.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="add">
      <el-button type="primary" @click="addIndustry">添加专利</el-button>
    </div>
    <div class="table">
      <el-table style="height: calc(100vh - 230px)" :data="tableData">
        <el-table-column type="index" width="60" label="序号"></el-table-column>
        <el-table-column
          prop="name"
          align="center"
          show-overflow-tooltip
          label="专利名称"
        ></el-table-column>
        <el-table-column
          prop="enterprise.name"
          align="center"
          show-overflow-tooltip
          label="企业名称"
        ></el-table-column>
        <el-table-column
          prop="applicant"
          align="center"
          show-overflow-tooltip
          label="申请人"
        ></el-table-column>
        <el-table-column
          prop="patentType"
          align="center"
          show-overflow-tooltip
          label="专利类型"
        ></el-table-column>
        <el-table-column
          prop="applicationDate"
          align="center"
          show-overflow-tooltip
          label="申请日期"
        >
          <template #default="scope">
            <span>{{ new Date(scope.row.applicationDate).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="registrationDate"
          align="center"
          show-overflow-tooltip
          label="注册日期"
        >
          <template #default="scope">
            <span>{{ new Date(scope.row.applicationDate).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="abstract" align="center" show-overflow-tooltip label="专利摘要">
          <template #default="scope">
            {{ scope.row.abstract ? scope.row.abstract : '暂无描述' }}
          </template>
        </el-table-column>
        <el-table-column prop="operation" label="操作">
          <template #default="scope">
            <el-button type="text" @click="editPatent(scope.row)">编辑</el-button>
            <el-button type="text" @click="deletePatent(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="tableData.length" class="pagination-w">
        <el-pagination
          :total="searchParams.total"
          background
          :locale="zhCn"
          :current-page="searchParams.current"
          :page-size="searchParams.pageSize"
          :page-sizes="searchParams.pageSizes"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <AddPatentDialog ref="addPatentDialog" @submit="getPatentData" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { getPatentList, deletePatentAPI } from '@/api/enterprise'
import AddPatentDialog from './addPatentView.vue'
import { ElMessage } from 'element-plus'

const form = reactive({
  name: '',
})
const tableData = ref([])
const searchParams = ref({
  current: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 200],
  total: 0,
})
const search = async () => {
  const res = await getPatentList(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.patents
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const addPatentDialog = ref(null)

const editPatent = (row) => {
  addPatentDialog.value?.openDialog(row)
}

const addIndustry = () => {
  addPatentDialog.value?.openDialog()
}

const getPatentData = async () => {
  const res = await getPatentList(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.patents
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const handleSizeChange = (newSize) => {
  console.log('size change', newSize)
  searchParams.value.pageSize = newSize
  getPatentData()
}

const deletePatent = async (row) => {
  const res = await deletePatentAPI(row.id)
  if (res.status === 200 || res.status === 201) {
    ElMessage.success('删除成功')
    getPatentData()
  }
}

const handlePageChange = (newPage) => {
  console.log('page change', newPage)
  searchParams.value.current = newPage
  getPatentData()
}
onMounted(() => {
  getPatentData()
})
</script>

<style scoped lang="scss">
.enterprisePatent {
  padding: 30px;
  position: relative;
  .add {
    position: absolute;
    top: 30px;
    right: 30px;
  }
  .table {
    .pagination-w {
      display: flex;
      justify-content: right;
      margin-top: 20px;
    }
  }
}
</style>
